package com.xh.one2one.knowledgeSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.one2one.knowledgeSystem.service.IKnowledgeElementService;
import com.xh.one2one.knowledgeSystem.model.KnowledgeElement;
import com.xh.one2one.knowledgeSystem.bean.request.KnowledgeElementRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

import com.xh.core.controller.BaseController;

/**
 * <p>
 * 知识元体系 前端控制器
 * </p>
 *
 * @author tw
 * @since 2020-12-22
 */
@RestController
@RequestMapping("/admin/knowledgeElement")
public class KnowledgeElementController extends BaseController {

    @Autowired
    private IKnowledgeElementService knowledgeElementService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, KnowledgeElementRequest bean){
        page=knowledgeElementService.queryKnowledgeElementPage(page,bean);
        return page;
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(KnowledgeElementRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",knowledgeElementService.queryKnowledgeElement(bean));
    }

    /**
     * 查询知识元树形结构
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryTree")
    public AjaxResult queryTree(KnowledgeElementRequest bean){
        if(StringUtils.isEmpty(bean.getSubjectId())){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",knowledgeElementService.queryTree(bean.getSubjectId()));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(KnowledgeElementRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=knowledgeElementService.exportAll(request,exportHeaderBeans,bean,"queryKnowledgeElementPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param knowledgeElement 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated KnowledgeElement knowledgeElement){
        if(StringUtils.isEmpty(knowledgeElement.getId())){
            QueryWrapper<KnowledgeElement> qw = new QueryWrapper();
            qw.eq("pid",knowledgeElement.getPid());
            Integer count = knowledgeElementService.selectCount(qw);
            knowledgeElement.setSortNum(count+1);
            knowledgeElementService.insert(knowledgeElement);
        }else{
            knowledgeElementService.update(knowledgeElement);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @PostMapping(value="/save")
    public AjaxResult save(@Validated @RequestBody List<KnowledgeElementRequest> request){
        if(CollectionUtils.isEmpty(request)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        knowledgeElementService.save(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @PostMapping(value="/updateSort")
    public AjaxResult update(@Validated @RequestBody List<KnowledgeElementRequest> request){
        if(CollectionUtils.isEmpty(request)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        knowledgeElementService.updateSort(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        knowledgeElementService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        knowledgeElementService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param id
     * @return
     */
    @PostMapping("/batchDeleteByParentId")
    public AjaxResult batchDeleteByParentId(String id){
        knowledgeElementService.batchDeleteByParentId(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

